import dash
import time
from dash import html
import feffery_antd_components as fac
from dash.dependencies import Input, Output

app = dash.Dash(__name__)

app.layout = html.Div(
    [
        fac.AntdButton(
            '点我运算',
            id='demo-button',
            type='primary'
        ),
        fac.AntdSpin(
            fac.AntdText(
                id='demo-output'
            )
        )
    ]
)


@app.callback(
    Output('demo-output', 'children'),
    Input('demo-button', 'nClicks')
)
def demo(nClicks):
    # 开始计时
    start_time = time.time()
    time_cost_list = []

    if nClicks:
        # 假装在执行耗时分析任务
        time.sleep(1)

        # 第一次埋点
        time_cost_list.append(time.time() - start_time)

        # 假装在执行耗时分析任务
        time.sleep(2)

        # 第二次埋点
        time_cost_list.append(time.time() - start_time)

        # 假装在执行耗时分析任务
        time.sleep(3)

        # 第三次埋点
        time_cost_list.append(time.time() - start_time)

        return str([
            f'第{i}段耗时：{round(t, 3)}秒' for i, t in enumerate(time_cost_list)
        ])


if __name__ == '__main__':
    app.run_server(debug=True)
